Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
The essential building block for command line tables: truncate & pad strings to given width, taking care of wide characters, accents and ANSI colors
The essential building block for command line tables: truncate & pad strings to given width, taking care of wide characters, accents and ANSI colors.
Table of Contents generated with DocToc
{ to_width, width_of, } = require 'to-width'
width_of
is provided by
sindresorhus/string-width; it provides a fairly
reliable way to determine the width of strings on character devices. All people who
deal with string.length
, encodings and buffers in JavaScript will enjoy the
following table:
string | string.length | Buffer.byteLength string | width_of string |
---|---|---|---|
'abcd' | 4 ✅ | 4 ✅ | 4 ✅ |
'äöüß' | 4 ✅ | 8 ❌ | 4 ✅ |
'äöüß' (using combining diacritics) | 7 (✅) | 11 ❌ | 7 ❌ |
'北京' | 2 (✅) | 6 ❌ | 4 ✅ |
'𪜀𪜁' | 4 ❌ | 8 ❌ | 4 ✅ |
width_of
doesn't correctly count combining characters.to_width
, and combining
diacritics may get lost:'#' + ( to_width 'abcdabcd', 4 ) + '#' # --> #abc…#
'#' + ( to_width 'äöüßäöüß', 4 ) + '#' # --> #äöü…#
'#' + ( to_width 'äöüßäöüß', 4 ) + '#' # --> #äöu…#
'#' + ( to_width '北京北京', 4 ) + '#' # --> #北……#
'#' + ( to_width '𪜀𪜁𪜀𪜁', 4 ) + '#' # --> #𪜀�…#
When I needed tabular data display on the command line, I got dissatisfied with existing solutions. There are some promising modules for doing this on npm, but nothing satisfied me in the end.
I realized that the key requirement for doing tables in the terminal is the ability to format data so that each chunk of text (that you build table cells with) has exactly the correct visual width. Actually, string length fitting seems to have become quite the rage among people these days, at least judging by the recent left-pad hype.
The core functionality of this module has been implemented using
These two modules do the heavy lifting (looking for wide characters, combining characters, and
ANSI color codes); to-width
does only a little bit of glueing (and fixing
providing a workaround for a minor bug in wcstring
).
These packages have also been considered:
FAQs
The essential building block for command line tables: truncate & pad strings to given width, taking care of wide characters, accents and ANSI colors
The npm package to-width receives a total of 22 weekly downloads. As such, to-width popularity was classified as not popular.
We found that to-width demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.